﻿using System.ComponentModel;
using System.Windows.Media;
using Loki.UI;
using Loki.Utils;
using Luna.Model.CRM;

namespace Luna.UI.CRM
{
    public class DisplayableCategory : LunaDisplayable
    {
        #region Name

        private static PropertyChangedEventArgs argsNameChanged = ObservableHelper.CreateChangedArgs<DisplayableCategory>(x => x.Name);

        private static PropertyChangingEventArgs argsNameChanging = ObservableHelper.CreateChangingArgs<DisplayableCategory>(x => x.Name);

        private string name;

        public new string Name
        {
            get
            {
                return name;
            }

            set
            {
                if (value != name)
                {
                    NotifyChanging(argsNameChanging);
                    name = value;
                    NotifyChangedAndDirty(argsNameChanged);
                }
            }
        }

        #endregion Name

        #region Description

        private static PropertyChangedEventArgs argsDescriptionChanged = ObservableHelper.CreateChangedArgs<DisplayableCategory>(x => x.Description);

        private static PropertyChangingEventArgs argsDescriptionChanging = ObservableHelper.CreateChangingArgs<DisplayableCategory>(x => x.Description);

        private string description;

        public new string Description
        {
            get
            {
                return description;
            }

            set
            {
                if (value != description)
                {
                    NotifyChanging(argsDescriptionChanging);
                    description = value;
                    NotifyChangedAndDirty(argsDescriptionChanged);
                }
            }
        }

        #endregion Description

        #region Color

        private static PropertyChangedEventArgs argsColorChanged = ObservableHelper.CreateChangedArgs<DisplayableCategory>(x => x.Color);

        private static PropertyChangingEventArgs argsColorChanging = ObservableHelper.CreateChangingArgs<DisplayableCategory>(x => x.Color);

        private Color color;

        public new Color Color
        {
            get
            {
                return color;
            }

            set
            {
                if (value != color)
                {
                    NotifyChanging(argsColorChanging);
                    color = value;
                    NotifyChangedAndDirty(argsColorChanged);
                }
            }
        }

        #endregion Color

        public DisplayableCategory()
        {
            AddValidator<DisplayableCategory, string>(x => x.Name, Rules.Required, "Le nom est requis.");
        }

        public IEditableCategory Category
        {
            get;
            set;
        }

        public void LoadValues()
        {
            BeginInit();
            ID = Category.ID;
            Name = Category.Name;
            Description = Category.Description;
            Color = (Color)ColorConverter.ConvertFromString(Category.Color);
            EndInit();
        }

        public void CollectValues()
        {
            Category.Name = Name;
            Category.Description = Description;
            Category.Color = Color.ToString();
        }
    }
}